package ngat.oss;

import ngat.net.*;
import ngat.util.*;
import ngat.message.base.*;
import ngat.message.OSS.*;
import ngat.phase2.*;
import ngat.instrument.*;
import dev.lt.*;

public class ScheduleTestUnit implements JMSClientProtocolResponseHandler {

    private String host;

    private int    port;

    private long   maxIdle;

      public ScheduleTestUnit(String host, int port, long maxIdle) {
	this.host    = host;
	this.port    = port;
	this.maxIdle = maxIdle;
    }

    public static void main(String args[]) {

	try {

	    CommandTokenizer parser = new CommandTokenizer("--");
	    parser.parse(args);
	    ConfigurationProperties config = parser.getMap();

	    String host = config.getProperty("host");
	    int    port = config.getIntValue("port");

	    long maxIdle = config.getLongValue("max-idle");
	    
	    ScheduleTestUnit client = new ScheduleTestUnit(host, port, maxIdle);

	    int runs = config.getIntValue("runs", 1);
	    for (int i = 0; i < runs; i++) {
		client.exec();
	    }

	} catch (Exception e) {
	    e.printStackTrace();
	    System.err.println("Usage: ScheduleClient --host <host> --port <port> -max-idle <ms>");
	    return;
	}
    }

    public void exec() throws Exception {

	long now = System.currentTimeMillis();

	RATCamConfig config = new RATCamConfig("test-config");
	config.setLowerFilterWheel("clear");
	config.setUpperFilterWheel("clear");
	CCDDetector detector = (CCDDetector)config.getDetector(0);
	detector.setXBin(2);
	detector.setYBin(2);
	detector.clearAllWindows();
	INSTRUMENT_INFO info = new INSTRUMENT_INFO(
						   "schedule-client-test",
						   config,
						  "RATCAM",
						   true,
						   Instrument.NETWORK_STATUS_ONLINE,
						   Instrument.OPERATIONAL_STATUS_OKAY);
	info.setClientDescriptor(new ClientDescriptor("/RCS", 
							  ClientDescriptor.RCS_CLIENT, 
						      ClientDescriptor.RCS_PRIORITY));
	info.setCrypto(new Crypto("/RCS"));

	JMSClientProtocolImplementor jms = new JMSClientProtocolImplementor();
	jms.setTimeout(6000L);
	jms.implementProtocol(this,
			      new SocketConnection(host, port),
			      info);

	try {Thread.sleep(1000L);} catch (InterruptedException x) {}

	TEL_INFO tel = new TEL_INFO(
				    "schedule-client-test",
				    Math.toRadians(180.0),
				    Math.toRadians(45.0),
				    true,
				    Math.toRadians(-180.0),
				    Math.toRadians(350.0),
				    Math.toRadians(20.0),
				    Math.toRadians(85.0),
				    Math.toRadians(-240.0),
				    Math.toRadians(240.0));

	tel.setClientDescriptor(new ClientDescriptor("/RCS", 
							  ClientDescriptor.RCS_CLIENT, 
							  ClientDescriptor.RCS_PRIORITY));
	tel.setCrypto(new Crypto("/RCS"));

	jms = new JMSClientProtocolImplementor();
	jms.setTimeout(60000L);
	jms.implementProtocol(this,
			      new SocketConnection(host, port),
			      tel);
	try {Thread.sleep(1000L);} catch (InterruptedException x) {}

	UPDATE_SEEING see = new  UPDATE_SEEING(
					       "schedule-client-test",
					       UPDATE_SEEING.UPDATE_SEEING,
					       0.6,
					       now);
	see.setClientDescriptor(new ClientDescriptor("/RCS", 
							  ClientDescriptor.RCS_CLIENT, 
							  ClientDescriptor.RCS_PRIORITY));
	see.setCrypto(new Crypto("/RCS"));


	jms = new JMSClientProtocolImplementor();
	jms.setTimeout(100000L);
	jms.implementProtocol(this,
			      new SocketConnection(host, port),
			      see);
	try {Thread.sleep(1000L);} catch (InterruptedException x) {}
	

	SCHEDULE request = new SCHEDULE(
	         "schedule-client-test",
	         maxIdle,
		 now + 24*3600*1000L,
	         0L,
	         "OPTIMAL",
	         false,
	         "TEST");
	request.setClientDescriptor(new ClientDescriptor("/RCS", 
							  ClientDescriptor.RCS_CLIENT, 
							  ClientDescriptor.RCS_PRIORITY));
	request.setCrypto(new Crypto("/RCS"));

	jms = new JMSClientProtocolImplementor();
	jms.setTimeout(120000L);
	jms.implementProtocol(this,
			      new SocketConnection(host, port),
			      request);
    }
    
    public void handleAck(ACK ack) throws Exception {
	System.err.println("Ack received: "+ack+", timeout: "+ack.getTimeToComplete()+" ms");
    }

    public void handleDone(COMMAND_DONE done) throws Exception {
	System.err.println("Done received:"+done.getClass().getName()+" : "+
			   (done.getSuccessful() ? "Success" : "Failed"+done.getErrorNum()+":"+done.getErrorString()));
	if (done instanceof SCHEDULE_DONE) {
	    SCHEDULE_DONE sd = (SCHEDULE_DONE)done;
	    Group group = sd.getGroup();
	    System.err.println("Result="+group);
	}
	
    }

}
